home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 00 - Game Programming Primer / HelloWorld5.c < prev    next >
Text File  |  1995-06-29  |  26KB  |  555 lines

  1. //==============================================================================================\\
  2. //        -----------------------------------------------------------------------------------        \\
  3. //        HelloWorld5.c version 1.0.0    copyright © 1993…1995 Jamie McCornack, john calhoun            \\
  4. //        -----------------------------------------------------------------------------------        \\
  5. //         Demo program for Macintosh GameWriter 1.0.0, a training program…                        \\
  6. //        …for beginning Mac game programmers. MGW1 includes MGWExterns1.h, MGWUtilities1.c,…        \\
  7. //        …MGWSound1.c, MGWGraphics1.c, MGWGraphicsBWLite1.c, HelloWorld.rsrc and an assortment…    \\
  8. //        …of demo programs; projects HelloWorld1.π etc. and source code files HelloWorld1.c etc.    \\
  9. //         A tutorial is available in Tricks of the Mac Game Programming Gurus, published…        \\
  10. //        …by Hayden Books, August 1995.                                                            \\
  11. //                                                                                                \\
  12. //        This code is offered by the copyright holders for no fee and for whatever use…            \\
  13. //        …you care to make of it, but we do hope you remember where it came from.                \\
  14. //                                                                                                \\
  15. //        Please send bug reports to MacGameDev at America OnLine.    macgamedev@aol.com            \\
  16. //        Suggestions and observations are also appreciated.                                        \\
  17. //        Updates and upgrades will be available now and then from the above e-mail address.        \\
  18. //==============================================================================================\\
  19.  
  20. // This program opens a window, displays a color background suitable for whatever color or…
  21. // …grayscale depth the main monitor is set for, and at first…
  22. // …mouseclick, runs two clams across the screen. At second mouseclick, one clam stops,…
  23. // …talks, and flaps its/his/her face for 40 frames as the other clam continues running.
  24. // When the other clam runs up to the standing clam, there is an impact.
  25.  
  26. // Note: departure from text--for monitors deeper than 8-bit, default is 256 colors. The PICTs…
  27. // …get huge over 8-bit, and you get annoyances like heap space problems.
  28.  
  29.  
  30. #include "MGWExterns1.h"
  31.  
  32. #define     kPutInFront    (WindowPtr)-1L
  33. #define     kWaitTicks     4L        // Sets the delay in Ticks between frames. Try 3. Try 2. Try 0.
  34. #define     kJaneStepLength    12    // Sets the distance in pixels between Clamity Jane's moves.
  35. #define     kClemStepLength    16    // Sets the distance in pixels between Clem the Clam's moves.
  36. #define  kFrontFace            0    // Enumerates the various faces of the clam sprites.
  37. #define  kBlinkFace         1
  38. #define  kEehFace            2
  39. #define  kOohFace            3
  40. #define  kStepRightFace        4
  41. #define  kWalkRightFace        5
  42. #define  kRunRightFace        6
  43. #define  kDizzyRightFace1    7
  44. #define  kDizzyLeftFace1    8
  45. #define  kDizzyRightFace2    9
  46. #define  kDizzyLeftFace2    10
  47. #define  kMaxFaces            11
  48. #define  kBounce            -2    // Sets how many steps the running clam bounces back on impact.
  49. #define  kLastFace        40        // Sets how many frames the clams stay visible after impact.
  50.         
  51.                 // The resource constants--with 'r' prefix like Apple wants them.
  52. #define  rJaneFaces1ID    129        // The 'PICT' ID# where the views of Jane are located.
  53. #define  rClemFaces1ID    129        // The 'PICT' ID# where the views of Clem are located.
  54. #define  rBackground1ID    128        // The 'PICT' ID# where the background picture is located.
  55. #define  rJaneFaces4ID    133        // The 'PICT' ID# where the views of Jane are located.
  56. #define  rClemFaces4ID    132        // The 'PICT' ID# where the views of Clem are located.
  57. #define  rBackground4ID    131        // The 'PICT' ID# where the background picture is located.
  58. #define  rJaneFaces8ID    136        // The 'PICT' ID# where the views of Jane are located.
  59. #define  rClemFaces8ID    135        // The 'PICT' ID# where the views of Clem are located.
  60. #define  rBackground8ID    134        // The 'PICT' ID# where the background picture is located.
  61. #define  rMasksID        130        // The 'PICT' ID# where the clam masks are located.
  62. #define  rMainWindowID    128        // The 'WIND' resource ID# for the main window.
  63.  
  64. #define  rHelloSndID    3000
  65. #define  rFootstepSndID    3001
  66. #define  rImpactSndID    3002
  67. #define  rDizzySndID    3003
  68.  
  69. // #define  kColorBitsNeeded    8
  70.     
  71. typedef struct
  72. {
  73.     Rect    face;
  74.     Rect    mask;
  75. } tSpriteType;
  76.  
  77.  
  78. Rect    bigPictureRect, masksRect,  allComboRect;
  79. Rect    janeFacesRect, janeIsAtRect, janeWasAtRect, janeComboRect;
  80. Rect    clemFacesRect, clemIsAtRect, clemWasAtRect, clemComboRect;
  81. CGrafPtr    workCPort, janeFacesCPort, clemFacesCPort, backgroundCPort;
  82. GrafPtr mainWindow, masksPort;
  83. Boolean    itWorked, contactFlag, impactReadyFlag, gameOverFlag, evenFrame;    // Note new flags.
  84. long    targetTick;
  85. short    janeSprite, clemSprite, thisFaceCounter;
  86. short    bestJaneFacesID, bestClemFacesID, bestBackgroundID;
  87.  
  88. tSpriteType    sprite[kMaxFaces];
  89.  
  90. extern    Boolean        gUserWantsSound;
  91.     
  92.     
  93. //==============================================================  Prototypes
  94.  
  95. void InitAll(void);
  96. void SetBestPICTs (void);
  97. void OpenMainWindow (void);
  98. void SetTheRects(void);
  99. void SetTheCPorts(void);
  100. void CopyBothAtOnce (void);
  101. void CopyOneAtATime (void);
  102. void ShowClams (void);
  103. void DoDelay (void);
  104. void JaneLoop (void);
  105. void ClemLoop (void);
  106. void JaneSpeaks (void);
  107. void DoImpact (void);
  108.  
  109. //==============================================================  Functions
  110.  
  111. //--------------------------------------------------------------  InitAll
  112.  
  113. void InitAll(void)
  114. {
  115.     InitToolbox();
  116. //    if (WhatsOurDepth() != kColorBitsNeeded)    // Don't need this right now,…
  117. //        RedAlert(kErrNot8BitColor);                // …since we're not insisting on any particular depth.
  118.     SetBestPICTs();
  119.     gUserWantsSound = TRUE;
  120.     InitializeForSound();
  121.     SetTheRects();        // Since some of these Rects define fields in CGrafPorts,…
  122.     SetTheCPorts();        // …set the rects before setting the ports.
  123.     janeSprite = kStepRightFace;
  124.     clemSprite = kRunRightFace;
  125.     thisFaceCounter = 0;
  126.     contactFlag = FALSE;        // Not touching,
  127.     impactReadyFlag = FALSE;    // Not about to collide,
  128.     gameOverFlag = FALSE;        // Not done playing.
  129.     evenFrame = TRUE;
  130.     targetTick = TickCount() + kWaitTicks;
  131.     HideCursor();        // This demo doesn't use mouse input, so why look at it?
  132. }
  133.  
  134. //--------------------------------------------------------------  SetBestPICTs
  135.  
  136. void SetBestPICTs (void)    // Selects the best PICTs for current monitor pixel depth.
  137. {    short thisDepth;
  138.     thisDepth = WhatsOurDepth();
  139.     switch    (thisDepth)                    
  140.     {    case    (1):             // Black and white, which won't run with pixmaps. Gotta quit.
  141.         {    RedAlertString("\pThis program requires color or grayscale");    // Our error routine.
  142.             break;    }
  143.         case    (2):             // 4 grays.
  144.         {    bestBackgroundID = rBackground1ID;
  145.             bestClemFacesID = rClemFaces1ID;
  146.             bestJaneFacesID = rJaneFaces1ID;
  147.             break;    }
  148.         case    (4):             // 16 colors or grays.
  149.         {    bestBackgroundID = rBackground4ID;
  150.             bestClemFacesID = rClemFaces4ID;
  151.             bestJaneFacesID = rJaneFaces4ID;
  152.             break;    }
  153.         default:             // Millions of colors.
  154.         {    bestBackgroundID = rBackground8ID;
  155.             bestClemFacesID = rClemFaces8ID;
  156.             bestJaneFacesID = rJaneFaces8ID;
  157.             break;    }
  158.     }
  159. }
  160.  
  161. //--------------------------------------------------------------  OpenMainWindow
  162.  
  163. void OpenMainWindow (void)
  164. {
  165.     mainWindow = GetNewCWindow(128, 0L, kPutInFront);    // Load window from resource.
  166.     ShowWindow((GrafPtr)mainWindow);                    // Now display it.
  167.     SetPort((GrafPtr)mainWindow);                        // Make its port current.
  168.     ClipRect(&bigPictureRect);                            // Set its clip region.
  169.     CopyRgn(mainWindow->clipRgn, mainWindow->visRgn);    // Set its visRgn.
  170.     ForeColor(blackColor);                                // Set its pen color to black.
  171.     BackColor(whiteColor);                                // Set background color white.
  172. }
  173.  
  174. //--------------------------------------------------------------  SetTheRects
  175.  
  176. void SetTheRects(void)    // The most tedious part of programming this type of game.
  177. {
  178.     SetRect(&janeFacesRect, 0, 0, 448, 64);        // Size and shape of BitMap for the sprite faces.
  179.     SetRect(&clemFacesRect, 0, 0, 448, 64);        // Size and shape of BitMap for the sprite faces.
  180.     SetRect(&masksRect, 0, 0, 448, 32);        // Size and shape of BitMap for the sprite masks.
  181.     SetRect(&bigPictureRect, 0, 0, 512, 322);    // The shape of the picture we'll put in the main window, workCPort and backgroundCPort.}
  182.     SetRect(&janeIsAtRect, 80, 240, 112, 272);    // The shape (32 x 32) of the images of Jane, and the position of the first image.}
  183.     janeWasAtRect = janeIsAtRect;                // Initializing janeIsAtRect...it has to start somewhere, and this is handy.}
  184.     janeComboRect = janeIsAtRect;
  185.     SetRect(&clemIsAtRect, 40, 244, 72, 276);    // The shape (32 x 32) of the images of Clem, and the position of the first image.}
  186.     clemWasAtRect = clemIsAtRect;                // Initializing clemIsAtRect...it has to start somewhere, and this is handy.}
  187.     clemComboRect = clemIsAtRect;
  188.         // And now, the tedium. In this sample, all we're doing is showing the clam running across the screen to the right.}
  189.         // However, if you use ResEdit and look at 'PICT' 129 in Sample.rsrc, you'll find 28 different views of the clam.}
  190.         // If we wanted the clam to run left too, and walk slowly, and face the user, and blink its eyes, we'd be calling…}
  191.         // …SetRect 56 times--one face and one mask per sprite. And if we had jumping clams and rear views of clams…}
  192.         // …and starfish and clamdiggers and other hazards of the clam environment, we might have hundreds of rects to set.}
  193.     SetRect(&sprite[kFrontFace].face, 320, 32, 352, 64);    // The shape and position of sprite[kFrontFace].face on facesCPort.portPixMap.
  194.     SetRect(&sprite[kFrontFace].mask, 320, 0, 352, 32);    // The shape and position of sprite[kFrontFace].mask on masksPort.portPixMap.
  195.     SetRect(&sprite[kBlinkFace].face, 320, 0, 352, 32);    // Note that some faces (e.g. eyes open or closed) use the same mask,…
  196.     SetRect(&sprite[kBlinkFace].mask, 320, 0, 352, 32);    // …since they have the same silhouette.}
  197.     SetRect(&sprite[kEehFace].face, 352, 0, 384, 32);    // I could write more comments here, but setting these rects…
  198.     SetRect(&sprite[kEehFace].mask, 352, 0, 384, 32);    // …is already tedious enough without a bunch of busy-work.
  199.     SetRect(&sprite[kOohFace].face, 352, 32, 384, 64);
  200.     SetRect(&sprite[kOohFace].mask, 352, 0, 384, 32);    
  201.     SetRect(&sprite[kStepRightFace].face, 192, 0, 224, 32);    
  202.     SetRect(&sprite[kStepRightFace].mask, 192, 0, 224, 32);    
  203.     SetRect(&sprite[kWalkRightFace].face, 224, 0, 256, 32);    
  204.     SetRect(&sprite[kWalkRightFace].mask, 224, 0, 256, 32);    
  205.     SetRect(&sprite[kRunRightFace].face, 160, 0, 192, 32);    // BTW, there are plenty more clam faces and masks in the 'PICT's,…
  206.     SetRect(&sprite[kRunRightFace].mask, 160, 0, 192, 32);    // …if you feel you need rect setting practice.  :-)
  207.     SetRect(&sprite[kDizzyRightFace1].face, 384, 0, 416, 32);        // Hey look! Here's more now!
  208.     SetRect(&sprite[kDizzyRightFace1].mask, 384, 0, 416, 32);    
  209.     SetRect(&sprite[kDizzyRightFace2].face, 384, 32, 416, 64);    
  210.     SetRect(&sprite[kDizzyRightFace2].mask, 384, 0, 416, 32);    
  211.     SetRect(&sprite[kDizzyLeftFace1].face, 416, 0, 448, 32);    
  212.     SetRect(&sprite[kDizzyLeftFace1].mask, 416, 0, 448, 32);    
  213.     SetRect(&sprite[kDizzyLeftFace2].face, 416, 32, 448, 64);    
  214.     SetRect(&sprite[kDizzyLeftFace2].mask, 416, 0, 448, 32);    
  215.     
  216. }
  217.  
  218. //--------------------------------------------------------------  SetTheCPorts
  219.  
  220. void SetTheCPorts(void)    // Create the CGrafPorts and load their .portPixMap fields.
  221. {
  222.             // Create BitMap for sprite masks. NOTE THIS IS A BITMAP!
  223.     CreateOffScreenBitMap (&masksRect, &masksPort);
  224.     LoadGraphic (rMasksID);        // …load 'PICT' resource for the clam masks.
  225.     
  226.             // Create PixMap for Jane faces.
  227.     CreateOffScreenPixMap (&janeFacesRect, &janeFacesCPort);
  228.     LoadGraphic (bestJaneFacesID);        // …load 'PICT' resource for the clam faces.
  229.  
  230.             // Create PixMap for Clem faces.
  231.     CreateOffScreenPixMap (&clemFacesRect, &clemFacesCPort);
  232.     LoadGraphic (bestClemFacesID);        // …load 'PICT' resource for the clam faces.
  233.  
  234.             // Create PixMap for the background.
  235.     CreateOffScreenPixMap (&bigPictureRect, &backgroundCPort);
  236.     LoadGraphic(bestBackgroundID);    // …load 'PICT' resource for the background picture.
  237.     
  238.             // Create PixMap for offscreen graphics work.
  239.     CreateOffScreenPixMap (&bigPictureRect, &workCPort);
  240.  
  241.     OpenMainWindow();
  242.  
  243.         //{This fills the main window with the background picture, so the user can see it.
  244.     CopyBits(&((GrafPtr)backgroundCPort)->portBits,
  245.         &((GrafPtr)mainWindow)->portBits, 
  246.         &bigPictureRect, &bigPictureRect, srcCopy, mainWindow->visRgn);
  247.  
  248. // This fills the workCPort.portPixMap with the background picture, so updates can be done quickly.
  249.     CopyBits(&((GrafPtr)backgroundCPort)->portBits,
  250.         &((GrafPtr)workCPort)->portBits, 
  251.         &bigPictureRect, &bigPictureRect, srcCopy, mainWindow->visRgn);
  252. }
  253.  
  254. //--------------------------------------------------------------  CopyBothAtOnce
  255.  
  256. void CopyBothAtOnce (void)    //Display the clams on the screen when they're close together.
  257. {
  258.         //UnionRect(clemComboRect, larryComboRect, allComboRect);
  259.         //{Find the smallest rectangle which will cover Larry and Clem.}
  260.     UnionRect(&janeComboRect, &clemComboRect, &allComboRect);
  261. // Find the smallest rectangle which will cover the old position of Clem and the new.
  262.         //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, allComboRect, allComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  263.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  264.         &(((GrafPtr)mainWindow)->portBits), 
  265.         &allComboRect, &allComboRect, srcCopy, mainWindow->visRgn);
  266.         //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, allComboRect, allComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  267.         //{Restore the workPort by covering up our clams with the background they obscure.  This way, workPort is…}
  268.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  269.         &(((GrafPtr)workCPort)->portBits), 
  270.         &allComboRect, &allComboRect, srcCopy, mainWindow->visRgn);
  271. // Restore the workCPort by covering up our clam with the background it obscures.
  272. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  273. // without having to copy the entire PixMap.
  274. }
  275.  
  276. //--------------------------------------------------------------  CopyOneAtATime
  277.  
  278. void CopyOneAtATime (void)    // Display the clams on the screen when they're far apart.
  279. {
  280.         //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, larryComboRect, larryComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  281.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  282.         &(((GrafPtr)mainWindow)->portBits), 
  283.         &janeComboRect, &janeComboRect, srcCopy, mainWindow->visRgn);
  284. // Copy the contents of janeComboRect from workCPort->portPixMap to the main window. In one swell foop, old Jane…
  285. //…will be erased, and the new Jane overlayed onto the background picture. Wallah! Flicker-free animation!
  286.     //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, clemComboRect, clemComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  287.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  288.         &(((GrafPtr)mainWindow)->portBits), 
  289.         &clemComboRect, &clemComboRect, srcCopy, mainWindow->visRgn);
  290. // Copy the contents of clemComboRect from workCPort->portPixMap to the main window. In one swell foop, old Clem
  291. //…will be erased, and the new Clem overlayed onto the background picture. Wallah! Flicker-free animation!
  292.     //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, larryRect, larryRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  293.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  294.         &(((GrafPtr)workCPort)->portBits), 
  295.         &janeIsAtRect, &janeIsAtRect, srcCopy, mainWindow->visRgn);
  296. // Restore the workCPort by covering up Jane's image with the background it obscures.
  297. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  298. // without having to copy the entire PixMap.
  299.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  300.         &(((GrafPtr)workCPort)->portBits), 
  301.         &clemIsAtRect, &clemIsAtRect, srcCopy, mainWindow->visRgn);
  302. // Restore the workCPort by covering up Clem's image with the background it obscures.
  303. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  304. // without having to copy the entire PixMap.
  305.         //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, clemRect, clemRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  306. }
  307.  
  308. //--------------------------------------------------------------  ShowClam 
  309.  
  310. void ShowClams (void)    // Do the animation and make it appear on the screen.
  311. {
  312.     Rect dummyRect;
  313.     
  314.     CopyMask(&((GrafPtr)janeFacesCPort)->portBits, 
  315.         &((GrafPtr)masksPort)->portBits, 
  316.         &((GrafPtr)workCPort)->portBits, 
  317.         &sprite[janeSprite].face, 
  318.         &sprite[janeSprite].mask, 
  319.         &janeIsAtRect);
  320. // Now there is an image of a clam in the new position in workMap.  If we had done this work in…
  321. // mainWindow, we would have seen considerable flickering.  Instead, we did it offscreen, and left the…
  322. // previous image of the clam visible on the screen while we worked.
  323.  
  324.     UnionRect(&janeWasAtRect, &janeIsAtRect, &janeComboRect);
  325. // Find the smallest rectangle which will cover the old position of Jane and the new.
  326.  
  327. CopyMask(&((GrafPtr)clemFacesCPort)->portBits, 
  328.         &((GrafPtr)masksPort)->portBits, 
  329.         &((GrafPtr)workCPort)->portBits, 
  330.         &sprite[clemSprite].face, 
  331.         &sprite[clemSprite].mask, 
  332.         &clemIsAtRect);
  333. // Now there is an image of a clam in the new position in workMap.  If we had done this work in…
  334. // mainWindow, we would have seen considerable flickering.  Instead, we did it offscreen, and left the…
  335. // previous image of the clam visible on the screen while we worked.
  336.  
  337.     UnionRect(&clemWasAtRect, &clemIsAtRect, &clemComboRect);
  338. // Find the smallest rectangle which will cover the old position of Clem and the new.
  339.  
  340.  
  341.     if (SectRect(&clemIsAtRect, &janeIsAtRect, &dummyRect))
  342.     {
  343.         contactFlag = TRUE;
  344.         CopyBothAtOnce();
  345.     }
  346.     else
  347.     {
  348.         contactFlag = FALSE;
  349.         CopyOneAtATime();
  350.     }
  351. }
  352. //    CopyBits(&((GrafPtr)workCPort)->portBits, 
  353.         //&(((GrafPtr)mainWindow)->portBits), 
  354.         //&clamComboRect, &clamComboRect, srcCopy, mainWindow->visRgn);
  355. // Copy the contents of comboRect from workCPort->portPixMap to the main window. In one swell foop, the old clam…}
  356. //…will be erased, and the new clam overlayed onto the background picture. Wallah! Flicker-free animation!}
  357.  
  358.     //CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  359.         //&(((GrafPtr)workCPort)->portBits), 
  360.         //&clamIsAtRect, &clamIsAtRect, srcCopy, mainWindow->visRgn);
  361. // Restore the workCPort by covering up our clam with the background it obscures.
  362. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  363. // without having to copy the entire PixMap.
  364. //}
  365.  
  366. //--------------------------------------------------------------  DoDelay
  367.  
  368. // This is the companion function to the above function (LogNextTick()).
  369. // We do nothing but loop until TickCount() catches up with (or passes) our…
  370. // global variable tickNext.
  371.  
  372. void DoDelay (void)
  373. {
  374.     do
  375.     {
  376.     }
  377.     while (TickCount() < targetTick);            // Loop until TickCount() catches up.
  378.     targetTick = TickCount() + kWaitTicks;
  379. }
  380.  
  381. //--------------------------------------------------------------  JaneLoop
  382.  
  383. void JaneLoop (void)
  384. {
  385.     switch    (janeSprite)                    //If the current view of jane is…
  386.     {    case    (kStepRightFace):             // …kStepRightFace, then set thisSprite to…
  387.         {    janeSprite = kWalkRightFace;    // …kWalkRightFace, and if it is currently…
  388.             break;    }
  389.         case    (kWalkRightFace):             // …kWalkFace, then set it to…
  390.         {    janeSprite = kRunRightFace;        // …kRunRightFace.
  391.             break;    }
  392.     // And if it was neither kStepRightFace nor kWalkRightFace, then janeSprite was either…
  393.         default                    :             // …kRunRightFace, or what it was when RunRight()…
  394.             janeSprite = kStepRightFace;    // …was called, so we set it to kStepRightFace
  395.     }
  396.     
  397.     janeWasAtRect = janeIsAtRect;            // Store the clam's current position as its last position,…
  398.                                             // …we'll be erasing it next time through the loop.
  399.     OffsetRect(&janeIsAtRect, kJaneStepLength, 0);    // Set the clam's next position--it'll be…
  400.                                             // …kStepLength pixels to the right of its last position.
  401.     if (janeIsAtRect.left > 512)                // If the clam has wandered out of sight,…
  402.     {                                        // …set the right border of clamIsAtRect…
  403.         janeIsAtRect.right = 0;                // …to the left edge of the screen…
  404.         janeIsAtRect.left = -32;            // …and move the left border of clamIsAtRect…
  405.     }                                        // …as needed to maintain its 32 x 32 shape & size.
  406. }
  407.  
  408. //--------------------------------------------------------------  ClemLoop
  409.  
  410. void ClemLoop (void)
  411. {
  412.     switch    (clemSprite)                    //If the current view of Clem is…
  413.     {    case    (kStepRightFace):             // …kStepRightFace, then set thisSprite to…
  414.         {    clemSprite = kWalkRightFace;    // …kWalkRightFace, and if it is currently…
  415.             break;    }
  416.         case    (kWalkRightFace):             // …kWalkFace, then set it to…
  417.         {    clemSprite = kRunRightFace;        // …kRunRightFace.
  418.             break;    }
  419.     // And if it was neither kStepRightFace nor kWalkRightFace, then clemSprite was either…
  420.         default                    :             // …kRunRightFace, or what it was when RunRight()…
  421.             clemSprite = kStepRightFace;    // …was called, so we set it to kStepRightFace
  422.     }
  423.     clemWasAtRect = clemIsAtRect;            // Store the clam's current position as its last position,…
  424.                                             // …we'll be erasing it next time through the loop.
  425.     OffsetRect(&clemIsAtRect, kClemStepLength, 0);    // Set the clam's next position--it'll be…
  426.                                             // …kStepLength pixels to the right of its last position.
  427.     if (clemIsAtRect.left > 512)                // If the clam has wandered out of sight,…
  428.     {                                        // …set the right border of clamIsAtRect…
  429.         clemIsAtRect.right = 0;                // …to the left edge of the screen…
  430.         clemIsAtRect.left = -32;            // …and move the left border of clamIsAtRect…
  431.     }                                        // …as needed to maintain its 32 x 32 shape & size.
  432.     if (clemSprite == kStepRightFace)                // Only sound a footstep in pose where foot strikes ground.
  433.           PlayASound(rFootstepSndID, kLowSoundPriority);        //Ahh, the pitter patter of tiny feet.
  434. }
  435.  
  436. //--------------------------------------------------------------  JaneSpeaks
  437.  
  438. // This routine has the clam stop moving, face the screen, and move its face.
  439.  
  440. void JaneSpeaks (void)
  441. {    Rect dummyRect;
  442.     if ((impactReadyFlag) && (SectRect(&janeIsAtRect, &clemIsAtRect, &dummyRect)))
  443.         DoImpact();
  444.     thisFaceCounter = thisFaceCounter + 1;
  445.          switch (thisFaceCounter)
  446.          {    case    (1): 
  447.             {    janeSprite = kFrontFace;
  448.                 break;    }
  449.             case    (2): 
  450.             {    janeSprite = kEehFace;
  451.                 break;    }
  452.             case    (3):     // This keeps an impact from occurring when both clams are in the same…
  453.             {    if (!(SectRect(&janeIsAtRect, &clemIsAtRect, &dummyRect)))    // …place when the…
  454.                     impactReadyFlag = TRUE;                                    // …button is pushed.
  455.                 break;    }
  456.             case    (4): 
  457.             {    janeSprite = kFrontFace;
  458.                 break;    }
  459.             case    (5): 
  460.             {    janeSprite = kOohFace;
  461.                 break;    }
  462.             case    (9): 
  463.             {    janeSprite = kEehFace;
  464.                 break;    }
  465.             case    (11): 
  466.             {    janeSprite = kOohFace;
  467.                 break;    }
  468.             case    (13): 
  469.             {    janeSprite = kFrontFace;
  470.                 break;    }
  471.             case    (16): 
  472.             {    janeSprite = kBlinkFace;
  473.                 break;    }
  474.             case    (18): 
  475.             {    janeSprite = kFrontFace;
  476.                 break;    }
  477.             case    (35): 
  478.             {    janeSprite = kBlinkFace;
  479.                 break;    }
  480.             case    (38): 
  481.             {    janeSprite = kFrontFace;
  482.                 break;    }
  483.             case    (39): 
  484.             {    impactReadyFlag = TRUE;
  485.                 break;    }
  486.         }
  487. }
  488.  
  489. //--------------------------------------------------------------  DoImpact
  490.  
  491. void DoImpact (void)
  492. {
  493.     janeWasAtRect = janeIsAtRect;    // Store the clam's current position as its last position.
  494.     OffsetRect(&janeIsAtRect, kJaneStepLength, 0);    // Set the clam's next position.
  495.     clemWasAtRect = clemIsAtRect;        // Store the clam's current position as its last position.
  496.     OffsetRect(&clemIsAtRect, kClemStepLength * kBounce, 0);    // Set the clam's next position.
  497.     PlayASound(rImpactSndID, kHighestSoundPriority);        // High enough to interrupt anything.
  498.     thisFaceCounter = 0;
  499.     while (thisFaceCounter <= kLastFace)
  500.     {
  501.         ShowClams();
  502.         DoDelay();                // Do nothing for a while.
  503.         PlayLoopSound(rDizzySndID, kHighSoundPriority);    // Not high enough to interrupt the impact.
  504.         thisFaceCounter = thisFaceCounter + 1;
  505.         evenFrame = !evenFrame;
  506.         if (evenFrame)
  507.         {
  508.             janeSprite = kDizzyLeftFace1;
  509.             clemSprite = kDizzyRightFace1;
  510.         }
  511.         else
  512.         {
  513.             janeSprite = kDizzyLeftFace2;
  514.             clemSprite = kDizzyRightFace2;
  515.         }
  516.     }
  517.     gameOverFlag = TRUE;        // And we're outta here. Game over.
  518. }
  519.  
  520. //--------------------------------------------------------------  main
  521. //----------------------------------------------------------------------
  522.  
  523. void main(void)
  524. {
  525.     InitAll();
  526.     while (!Button())    // Before the user presses the mouse button, nothing happens.
  527.         {
  528.         }
  529.     while (Button())    // When the user presses the mouse button, nothing happens.
  530.         {
  531.         }
  532.     while (!Button())    // When the mouse button is released, continue to…
  533.         {
  534.         ShowClams();    // Put the clams on the screen.
  535.         DoDelay();        // Keep everything from happening too fast.
  536.         JaneLoop();        // Put Jane in her next position.
  537.         ClemLoop();        // Put Clem in his next position.
  538.         }
  539.     PlayASound(rHelloSndID, kMediumSoundPriority);    // Sound, "Hello" when button is pushed.
  540.     while (!(gameOverFlag))    // …until the clams crash into each other and fall down dizzy.
  541.         {    
  542.         ShowClams();    // Put the clams on the screen.
  543.         DoDelay();        // Keep everything from happening too fast.
  544.         JaneSpeaks();    // Make mouth motions, shift into impact mode when ready.
  545.         ClemLoop();        // Put Clem in his next position.
  546.         }
  547.     CloseDownSound();
  548.     InitCursor();    // Rarely needed, since most programs call InitCursor on startup. Still, it's…
  549.                     // a good habit to leave everything in normal condition when your programs quit.
  550. }                    // And we're done.
  551.  
  552. //------------------------------------------------------------------------------------------\\
  553. //                                    End HelloWorld5.c                                        \\
  554. //------------------------------------------------------------------------------------------\\
  555.